const app = getApp();

Page({
  data: {
    startDate: '',
    endDate: '',
    overviewData: {
      total: 0,
      qualified: 0,
      unqualified: 0,
      concession: 0,
      qualified_rate: 0
    },
    processData: [],
    defectData: {
      items: [],
      counts: [],
      maxCount: 0
    }
  },

  onLoad() {
    // 设置默认日期（本月）
    const now = new Date();
    const endDate = this.formatDate(now);
    const startDate = this.formatDate(new Date(now.getFullYear(), now.getMonth(), 1));
    
    this.setData({
      startDate,
      endDate
    });
    
    this.loadData();
  },

  formatDate(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
  },

  onStartDateChange(e) {
    this.setData({
      startDate: e.detail.value
    });
  },

  onEndDateChange(e) {
    this.setData({
      endDate: e.detail.value
    });
  },

  loadData() {
    wx.showLoading({ title: '加载中...' });
    
    Promise.all([
      this.loadOverviewData(),
      this.loadProcessData(),
      this.loadDefectData()
    ]).finally(() => {
      wx.hideLoading();
    });
  },

  loadOverviewData() {
    return new Promise((resolve, reject) => {
      wx.request({
        url: app.globalData.baseUrl + '/getQualityStatistics',
        method: 'GET',
        header: {
          'Authorization': 'Bearer ' + app.globalData.token
        },
        data: {
          type: 'overview',
          start_date: this.data.startDate,
          end_date: this.data.endDate
        },
        success: (res) => {
          if (res.data.code === 1) {
            this.setData({
              overviewData: res.data.data
            });
            resolve();
          } else {
            wx.showToast({ title: res.data.msg, icon: 'none' });
            reject();
          }
        },
        fail: reject
      });
    });
  },

  loadProcessData() {
    return new Promise((resolve, reject) => {
      wx.request({
        url: app.globalData.baseUrl + '/getQualityStatistics',
        method: 'GET',
        header: {
          'Authorization': 'Bearer ' + app.globalData.token
        },
        data: {
          type: 'process',
          start_date: this.data.startDate,
          end_date: this.data.endDate
        },
        success: (res) => {
          if (res.data.code === 1) {
            this.setData({
              processData: res.data.data
            });
            resolve();
          } else {
            reject();
          }
        },
        fail: reject
      });
    });
  },

  loadDefectData() {
    return new Promise((resolve, reject) => {
      wx.request({
        url: app.globalData.baseUrl + '/getQualityStatistics',
        method: 'GET',
        header: {
          'Authorization': 'Bearer ' + app.globalData.token
        },
        data: {
          type: 'defect',
          start_date: this.data.startDate,
          end_date: this.data.endDate
        },
        success: (res) => {
          if (res.data.code === 1) {
            const data = res.data.data;
            // 计算最大值用于进度条
            data.maxCount = data.counts.length > 0 ? Math.max(...data.counts) : 1;
            
            this.setData({
              defectData: data
            });
            resolve();
          } else {
            reject();
          }
        },
        fail: reject
      });
    });
  }
});
